package com.example.zbh.dsliao.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMGroup;
import com.easemob.chat.EMGroupManager;
import com.example.zbh.dsliao.R;

import java.util.List;

/**
 * Created by zbh on 15/11/23.
 */
public class GroupAdapter extends BaseAdapter {
    private List<EMGroup> list;

    public void addData(List<EMGroup> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public boolean isLength() {
        return list != null && list.size() > 0;
    }

    @Override
    public int getCount() {
        return isLength() ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position).getGroupName();
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.group_listview_item, null);
        } else {
            view = convertView;
        }
        ViewHolder holder = ViewHolder.getViewHolder(view);
        if (isLength()) {
            holder.name_tv.setText(list.get(position).getGroupName());
            String id = list.get(position).getGroupId();
            if (EMGroupManager.getInstance().getGroup(id).getOwner().equals(EMChatManager.getInstance().getCurrentUser())){
                holder.image_huan.setVisibility(View.VISIBLE);
            }
        }
        return view;
    }

    public static class ViewHolder {
        private TextView name_tv;
        private ImageView image_huan;

        public static ViewHolder getViewHolder(View view) {
            ViewHolder holder = (ViewHolder) view.getTag();
            if (holder == null) {
                holder = new ViewHolder();
                holder.name_tv = (TextView) view.findViewById(R.id.name);
                holder.image_huan = (ImageView) view.findViewById(R.id.image_huan);
            }
            return holder;
        }

    }


}
